package com.whu.tcx01.hd.calNewRoad;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class RoadReducer extends Reducer<Text, RoadBean, Text, OutRoadBean> {

    // private OutRoadBean outV = new OutRoadBean();
    private OutRoadBean outV = new OutRoadBean();

    @Override
    protected void reduce(Text key, Iterable<RoadBean> values, Context context) throws IOException, InterruptedException {

        List<LineBean> list = new ArrayList<>();
        String[] split = key.toString().split("\t");
        if(split[0].equals(split[1])){
            return;
        }
        for (RoadBean value : values) {
            LineBean lineBean = new LineBean();
            lineBean.setRoadid(value.getRoadid());
            lineBean.setDirection(value.getDirection());
            lineBean.setDistance(value.getDistance());
            lineBean.setPathclass(value.getPathclass());
            lineBean.setSpeed(value.getSpeed());
            list.add(lineBean);
        }
        outV.setRoadid(MyConstants.count);
        outV.setStart(Long.parseLong(split[0]));
        outV.setStop(Long.parseLong(split[1]));
        outV.setLines(list);
        context.write(null, outV);
    }
}
